package controllers;

import play.*;
import play.db.jpa.JPABase;
import play.mvc.*;

import java.util.*;

import javax.naming.directory.SearchResult;

import models.*;

public class Application extends Controller {
	public static String Area;
	public static Date Date;
	public static ArrayList<Product> dirtyList = new ArrayList<Product>();
	public static ArrayList<Product> clean = new ArrayList<Product>();
	public static ArrayList<Product> newList = new ArrayList<Product>();
	public static ArrayList<Product> cancelList = new ArrayList<Product>();

	public static void index() {
		render();
	}

	public static void search() {
		render();
	}

	public static void create() {
		render();
	}

	public static void chooseDate() {
		render();
	}

	public static void showDate(Date date, String area) {
		ArrayList<Contract> allContracts = (ArrayList) Contract.findAll();
		ArrayList<Contract> returnContracts = new ArrayList<Contract>();
		ArrayList<Product> returnProducts = new ArrayList<Product>();
		ArrayList<Long> temp = new ArrayList<Long>();

		for (int i = 0; i < allContracts.size(); i++) {

			for (int j = 0; j < allContracts.get(i).plist.size(); j++) {

				if (allContracts.get(i).plist.get(j).change_time_list
						.contains(date)) {
					if (!temp.contains(allContracts.get(i).id)) {
						temp.add(allContracts.get(i).id);
						returnContracts.add(allContracts.get(i));
					}
					returnProducts.add(allContracts.get(i).plist.get(j));

				}
			}
		}
		// System.out.println("date " + date);
		render(returnContracts, returnProducts, area, date);

	}

	public static void showDate2(Date date, String area) {
		ArrayList<Contract> allContracts = (ArrayList) Contract.findAll();
		ArrayList<Product> returnProducts = new ArrayList<Product>();
		ArrayList<String> temp = new ArrayList<String>();

		int[] no_of_products = new int[5];
		for (int i = 0; i < allContracts.size(); i++) {

			for (int j = 0; j < allContracts.get(i).plist.size(); j++) {

				if (allContracts.get(i).plist.get(j).change_time_list
						.contains(date)) {
					// System.out.println("flag is "+returnProducts.contains("Mop"));
					if (!temp.contains(allContracts.get(i).plist.get(j).name)) {
						// temp.add(allContracts.get(i).plist.get(j).name);
						returnProducts.add(allContracts.get(i).plist.get(j));
						if (allContracts.get(i).plist.get(j).name
								.equalsIgnoreCase("Mop18")) {
							no_of_products[0] = no_of_products[0]
									+ allContracts.get(i).plist.get(j).quantity;
						}
						if (allContracts.get(i).plist.get(j).name
								.equalsIgnoreCase("Mit")) {
							no_of_products[1] = no_of_products[1]
									+ allContracts.get(i).plist.get(j).quantity;
						}
						if (allContracts.get(i).plist.get(j).name
								.equalsIgnoreCase("HD1")) {
							no_of_products[2] = no_of_products[2]
									+ allContracts.get(i).plist.get(j).quantity;
						}
						if (allContracts.get(i).plist.get(j).name
								.equalsIgnoreCase("HD2")) {
							no_of_products[3] = no_of_products[3]
									+ allContracts.get(i).plist.get(j).quantity;
						}
						if (allContracts.get(i).plist.get(j).name
								.equalsIgnoreCase("Mop36")) {
							no_of_products[4] = no_of_products[4]
									+ allContracts.get(i).plist.get(j).quantity;
						}
					}

				}
			}
		}

		ArrayList<myNumber> n = new ArrayList<myNumber>();
		if (no_of_products[0] != 0)
			n.add(new myNumber(no_of_products[0]));

		if (no_of_products[1] != 0)
			n.add(new myNumber(no_of_products[1]));

		if (no_of_products[2] != 0)
			n.add(new myNumber(no_of_products[2]));

		if (no_of_products[3] != 0)
			n.add(new myNumber(no_of_products[3]));

		if (no_of_products[4] != 0)
			n.add(new myNumber(no_of_products[4]));

		render(returnProducts, n);
	}

	public static void searchByPhone(String mobile) {
		ArrayList<Contract> allContracts = (ArrayList) Contract.findAll();
		ArrayList<Contract> returnContracts = new ArrayList<Contract>();

		for (int i = 0; i < allContracts.size(); i++) {
			if (allContracts.get(i).mobile1.equals(mobile)
					|| allContracts.get(i).mobile2.equals(mobile)
					|| allContracts.get(i).phone1.equals(mobile)
					|| allContracts.get(i).phone2.equals(mobile)) {
				returnContracts.add(allContracts.get(i));
			}
		}
		render(returnContracts);
	}

	public static void searchByArea(String area) {
		ArrayList<Contract> allContracts = (ArrayList) Contract.findAll();
		ArrayList<Contract> returnContracts = new ArrayList<Contract>();

		for (int i = 0; i < allContracts.size(); i++) {
			if (allContracts.get(i).area.equals(area)) {
				returnContracts.add(allContracts.get(i));
			}
		}
		render(returnContracts);
	}

	public static void carReturn_chooseDate() {

		render();

	}

	public static ArrayList<Product> calculate_renderProducts(String area,
			Date date) {
		ArrayList<Contract> allContracts = (ArrayList) Contract.findAll();
		ArrayList<Contract> returnContracts = new ArrayList<Contract>();
		ArrayList<Product> returnProducts = new ArrayList<Product>();
		ArrayList<Long> temp = new ArrayList<Long>();

		for (int i = 0; i < allContracts.size(); i++) {

			for (int j = 0; j < allContracts.get(i).plist.size(); j++) {

				if (allContracts.get(i).plist.get(j).change_time_list
						.contains(date)) {
					if (!temp.contains(allContracts.get(i).id)) {
						temp.add(allContracts.get(i).id);
						returnContracts.add(allContracts.get(i));
					}
					returnProducts.add(allContracts.get(i).plist.get(j));

				}
			}
		}
		return returnProducts;
	}

	public static void comeBack(String area, Date date) {
		ArrayList<Product> dirty = new ArrayList<Product>();
		ArrayList<Product> clean = new ArrayList<Product>();
		ArrayList<Product> newList = new ArrayList<Product>();
		ArrayList<Product> cancel = new ArrayList<Product>();

		Area = area;
		Date = date;
		ArrayList<Product> returnProducts = calculate_renderProducts(Area, Date);

		dirty = dirtyList;
		clean = Application.clean;
		newList = Application.newList;
		cancel = Application.cancelList;
		
		render(returnProducts, dirty, clean, newList, cancel);
	
	}

	public static boolean isNumeric(String str) {
		try {
			double d = Double.parseDouble(str);
		} catch (NumberFormatException nfe) {
			return false;
		}
		return true;
	}

	public static void carReturn(String CR, boolean NEW, boolean cancel) {
	ArrayList<Product> products = calculate_renderProducts(Area, Date);
		if (CR.equalsIgnoreCase("B") && !NEW && !cancel) {
			for (int i = 0; i < products.size(); i++) {
				clean.add(products.get(i));

			}
		} else if (isNumeric(CR) && !NEW && !cancel) {
			for (int i = 0; i < products.size(); i++) {
				dirtyList.add(products.get(i));
			}

		} else if (isNumeric(CR) && NEW && !cancel) {
			for (int i = 0; i < products.size(); i++) {
				newList.add(products.get(i));
			}

		} else if (cancel && CR.isEmpty() && !NEW) {

			for (int i = 0; i < products.size(); i++) {
				cancelList.add(products.get(i));
				dirtyList.add(products.get(i));
				//products.remove(i);
			}

		}
		System.out.println("dirty size " + dirtyList.size());
		comeBack(Area, Date);
		// render(products,dirty);
	}

}